<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Overtrue\Socialite\SocialiteManager;

class WechatOauthController extends Controller
{
    public function index(Request $request){
        if($request->input('code')){
            $redirect_uri = $request->input('callback', '');
            if(empty($redirect_uri)){
                abort(404);
            }else{
                return redirect($redirect_uri.'?code='.$request->input('code'));
            }
        }else{
            $wechat_config = config('wechat.official_account.default');
            $scope = $request->input('scope', 'snsapi_base');
            $config = [
                'wechat' => [
                    'client_id' => $wechat_config['app_id'],
                    'client_secret' => $wechat_config['secret'],
                    'redirect' => $request->fullUrl()
                ]
            ];
            $socialiteManager = new SocialiteManager($config);
            return $socialiteManager->driver('wechat')->scopes([$scope])->redirect();
        }
    }
}
